package com.example.springboot02amqp.util;

import com.example.springboot02amqp.config.MailConfig;
import com.example.springboot02amqp.model.MailBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.mail.MailProperties;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeUtility;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: 喵喵~
 * Date: 2022/3/22
 * Time: 8:45
 * Description:
 */
@Component
public class MailUtil {
    @Autowired
    JavaMailSender mailSender;
    @Autowired
    MailConfig mailConfig;
    /**
     * 从配置文件中注入发件人的姓名
     */
    @Value("${spring.mail.username}")
    private String fromEmail;

    @Autowired
    private TemplateEngine emailTemplateEngine;

    /**
     * 发送邮件测试方法
     */
    public void sendMail(MailBean mailBean) {
        SimpleMailMessage mimeMessage = new SimpleMailMessage();
        mimeMessage.setFrom(mailConfig.getUsername());
        mimeMessage.setTo(mailBean.getToAccount());
        mimeMessage.setSubject(mailBean.getSubject());
        mimeMessage.setText(mailBean.getContent());
        mailSender.send(mimeMessage);
    }

    /**
     * 发送html邮件
     */
    public void sendHtmlMail(String receiver, String subject, String content) {
        try {
            //注意这里使用的是MimeMessage
            MimeMessage message = mailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom(fromEmail);
            helper.setTo(receiver);
            helper.setSubject(subject);
            //第二个参数：格式是否为html
            helper.setText(content, true);
            mailSender.send(message);
        } catch (MessagingException e) {
            // log.error("发送邮件时发生异常！", e);
        }
    }

    /**
     * 发送模板邮件
     *
     * @param to
     * @param subject
     * @param template
     */
    public void sendTemplateMail(String to, String subject, String template) {
        Context ctx = new Context();
        ctx.setVariable("orderCode", "88888888");
        String htmlContent = emailTemplateEngine.process("msg.html", ctx);
        // 该方法本质上还是发送html邮件，调用之前发送html邮件的方法
        this.sendHtmlMail(to, subject, template);

    }

    /**
     * 发送邮件-附件邮件
     *
     * @param mailBean
     */
    public boolean sendMailAttachment(MailBean mailBean) {
        try {
            MimeMessage mimeMessage = mailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
            helper.setFrom(mailConfig.getUsername());
            helper.setTo(mailBean.getToAccount());
            helper.setSubject(mailBean.getSubject());
            helper.setText(mailBean.getContent(), true);
            // 增加附件名称和附件
            helper.addAttachment(MimeUtility.encodeWord(mailBean.getAttachmentFileName(), "utf-8", "B"), mailBean.getAttachmentFile());
            mailSender.send(mimeMessage);
            return true;
        } catch (MessagingException | UnsupportedEncodingException e) {
            e.printStackTrace();
            return false;
        }
    }
}
